<%-- 
    Document   : loginaction
    Created on : Dec 4, 2009, 4:14:48 PM
    Author     : gabriel
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="br.com.zumbits.avd.web.client.Conn" %>
<%@page import="br.com.zumbits.avd.web.client.Login" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Another Disk Virtual</title>
    </head>
    <body>
        <%
            Conn c = new Conn(Conn.SERVER_URL);

            String username = request.getParameter("username");
            String password = request.getParameter("password");

            Login login = c.login(username, password);
            if (login != null) {
                if (login.getStatus().equals(Conn.LOGIN_SUCCESS)) {
                    session.setAttribute("conn", c);
                    session.setAttribute("session_id", login.getSession());
                    session.setAttribute("username", username);
                    response.sendRedirect("startpage.jsp?currentPath=/");
                } else if (login.getStatus().equals(Conn.LOGIN_FAILURE)) {
                    response.sendRedirect("index.jsp");
                    session.setAttribute("message", "Wrong login or password");

                } else if (login.getStatus().equals(Conn.LOGIN_ALREADY)) {
                    response.sendRedirect("index.jsp");
                    session.setAttribute("message", "Already logged in");

                } else if (login.getStatus().equals(Conn.LOGIN_ERROR)) {
                    response.sendRedirect("index.jsp");
                    session.setAttribute("message", "Unknown error");

                }
            }

            out.print(login);
        %>
        <h1>Houve uma falha de conex&atilde;o com o servidor.<br>Por favor, tente novamente em alguns minutos.</h1>
    </body>
</html>
